// pages/compony-pages/user-card/user-card.js
const defaultFront = '../../../assets/image/identity_face.jpg';
const defaultLater = '../../../assets/image/identity_back.jpg';
import Toast from '../../../component/toast/toast';
const uploadFile = require('../../../lib/aliyun/uploadAliyun.js');
Page({

    data: {
        frontImg: defaultFront,
        laterImg: defaultLater
    },
    /**
     * ===================事件处理部分===================
     */
    clickSave(){
        let frontImg = this.data.frontImg;
        let laterImg = this.data.laterImg;
        if(frontImg == defaultFront){
            Toast("请上传身份证人像面");
            return;
        }
        if (laterImg == defaultLater) {
            Toast("请上传身份证国徽面");
            return;
        }
        let imgUrls = [];
        let that = this;
        let dir = 'xianjian_wx_image';
        wx.showLoading({
            title: '保存中...',
            mask: true
        });
        that.uploadSyncImg(frontImg, dir).then(res => {
            if (res.statusCode == 200) {
                imgUrls.push(res.imgUrl);
            }
            console.log("update 1：" + JSON.stringify(res));
            return res.statusCode;
        }, err => {
            wx.hideLoading();
            Toast("图片上传失败，请重新上传！");
            console.log("1" + JSON.stringify(err));
        }).then(statusCode => {
            if (statusCode == 200) {
                that.uploadSyncImg(laterImg, dir).then(res => {
                    if (res.statusCode == 200) {
                        imgUrls.push(res.imgUrl);
                    }
                    console.log("update 2：" + JSON.stringify(res));
                }, err => {
                    wx.hideToast();
                    Toast("图片上传失败，请重新上传！");
                    console.log("2" + JSON.stringify(err));
                }).then(() => {
                    console.log("图片上传完毕：" + imgUrls.length);
                    if (imgUrls.length == 2) {
                        Toast("图片保存成功！");
                        let pages = getCurrentPages();
                        let prevPage = pages[pages.length - 2];  //上一个页面
                        wx.navigateBack({
                            delta: 1,
                            success: function (res) {
                                prevPage.setData({
                                    authphoto: imgUrls
                                });
                            }
                        });
                    }
                });
            }
        });
    },
    chooseFrontImg(){
        let that = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['original'],
            sourceType: ['album', 'camera'],
            success(res) {
                that.setData({
                    frontImg: res.tempFilePaths[0]
                });
            }
        })
    },
    chooseLatertImg(){
        let that = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['original'],
            sourceType: ['album', 'camera'],
            success(res) {
                that.setData({
                    laterImg: res.tempFilePaths[0]
                });
            }
        });
    },
    /**
     * ===================事件处理部分===================
     */

    /**
     * -------------------网络请求部分-------------------
     */
    uploadSyncImg(path, dir) {
        let promis = new Promise(function (resolve, reject) {
            uploadFile({
                filePath: path,
                dir: dir,
                success: function (res) {
                    resolve(res);
                },
                fail: function (res) {
                    reject(res);
                }
            });
        });
        return promis;
    },
    /**
     * -------------------网络请求部分-------------------
     */

    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})